package com.ayoka.parenting.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.MeditationBean;
import com.ayoka.parenting.bean.MeditationGroupBean;
import com.ayoka.parenting.bean.MeditationMoreMultiEntity;
import com.ayoka.parenting.databinding.ItemMeditationIndexBinding;
import com.ayoka.parenting.ui.meditation.more.MeditationMoreContract;
import com.jincp.common.brvah.BaseMultiItemQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.StringUtil;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

import java.util.List;

/**
 * Created by jinchunping on 2018/4/11.
 */

public class MeditationMoreMultiAdapter extends BaseMultiItemQuickAdapter<MeditationMoreMultiEntity, BaseViewHolder> {

    public MeditationMoreMultiAdapter() {
        super(null);
        addItemType(MeditationMoreMultiEntity.TITLE, R.layout.multi_item_meditation_more_title);
        addItemType(MeditationMoreMultiEntity.ITEM, R.layout.multi_item_meditation_more_item);
    }

    @Override
    protected void convert(BaseViewHolder holder, final MeditationMoreMultiEntity item) {
        if (item.getItemType() == MeditationMoreMultiEntity.TITLE) {
            MeditationGroupBean meditationGroupBean = (MeditationGroupBean) item.getData();
            holder.setText(R.id.tv_meditation_title, meditationGroupBean.Title)
                    .setText(R.id.tv_meditation_description, meditationGroupBean.Descp);
        } else if (item.getItemType() == MeditationMoreMultiEntity.ITEM) {
            List<MeditationBean> meditationList = (List<MeditationBean>) item.getData();
            LinearLayout container = holder.getView(R.id.layout_meditation_container);
            container.removeAllViews();
            if (meditationList == null) return;
            for (MeditationBean meditationBean : meditationList) {
                View itemView = getMeditationView(meditationBean);
                container.addView(itemView);
            }
        }
    }

    private View getMeditationView(final MeditationBean meditationBean) {
        ItemMeditationIndexBinding meditationIndexBinding = ItemMeditationIndexBinding.inflate(LayoutInflater.from(mContext));
        ImageLoader.getInstance().displayBitmap(meditationBean.SmallImgUrl, meditationIndexBinding.ivPicture, ImageLoaderOptions.getRoundOptions());
        meditationIndexBinding.tvProgress.setText(meditationBean.OrderNum + "/21");
        meditationIndexBinding.tvMeditationTitle.setText(meditationBean.Title);
        meditationIndexBinding.tvMeditationDuration.setText(DateUtil.formatTime(meditationBean.MediaDuration));
        meditationIndexBinding.tvMeditationStudyCount.setText(StringUtil.formatString(R.string.format_study_count, meditationBean.Participant));
        meditationIndexBinding.tvLastStudyLabel.setVisibility(TextUtils.equals(lastMeditationId, meditationBean.ID) ? View.VISIBLE : View.GONE);
        meditationIndexBinding.btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (meditationMoreContractView != null) {
                    meditationMoreContractView.goToMeditationDetail(meditationBean.ID, meditationBean.Title, meditationBean.MediaType);
                }
            }
        });
        return meditationIndexBinding.getRoot();
    }

    private String lastMeditationId;

    public void setLastMeditationId(String lastMeditationId) {
        this.lastMeditationId = lastMeditationId;
    }

    private MeditationMoreContract.View meditationMoreContractView;

    public void setMeditationMoreContractView(MeditationMoreContract.View view) {
        this.meditationMoreContractView = view;
    }
}
